﻿using System;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;

namespace YiSha.Service.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2024-03-23 13:44
    /// 描 述：商品属性颜色服务类
    /// </summary>
    public class GoodsAttrService :  RepositoryFactory
    {
        #region 获取数据
        public async Task<List<GoodsAttrEntity>> GetList(GoodsAttrListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
        }

        public async Task<List<GoodsAttrEntity>> GetPageList(GoodsAttrListParam param, Pagination pagination)
        {
            var expression = ListFilter(param);
            var list= await this.BaseRepository().FindList(expression, pagination);
            return list.ToList();
        }

        public async Task<GoodsAttrEntity> GetEntity(long id)
        {
            return await this.BaseRepository().FindEntity<GoodsAttrEntity>(id);
        }
        #endregion

        #region 提交数据
        public async Task SaveForm(GoodsAttrEntity entity)
        {
            if (entity.Id.IsNullOrZero())
            {
                entity.Create();
                await this.BaseRepository().Insert(entity);
            }
            else
            {
                
                await this.BaseRepository().Update(entity);

                //同步货物颜色
                var hwdata=this.BaseRepository().FindList<GoodsWarehouseEntity>(t=>t.colorid==entity.Id).Result;

                foreach (var item in hwdata)
                {
                    item.color = entity.attr_name;
                    await this.BaseRepository().Update(item);
                }
            }
        }

        public async Task DeleteForm(string ids)
        {
            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
            await this.BaseRepository().Delete<GoodsAttrEntity>(idArr);
        }

        public async Task TBFormJson()
        {
            //初始化颜色信息
            //var ysdata=this.BaseRepository().FindList<GoodsAttrEntity>().Result.Select(t=>t.goods_id);
            //var goods=this.BaseRepository().FindList<GoodsEntity>(t =>!ysdata.Contains(t.Id)).Result;
            //foreach (var item in goods)
            //{
            //    GoodsAttrEntity attrEntity = new GoodsAttrEntity();
            //    attrEntity.attr_name = "亮光紫";
            //    attrEntity.is_del = 0;
            //    attrEntity.goods_id = item.Id;
            //    attrEntity.Create();
            //    await this.BaseRepository().Insert(attrEntity);

            //    var hwdata = this.BaseRepository().FindList<GoodsWarehouseEntity>(t => t.goods_id == item.Id).Result;

            //    foreach (var item1 in hwdata)
            //    {
            //        item1.color = attrEntity.attr_name;
            //        item1.colorid = attrEntity.Id.Value;
            //        await this.BaseRepository().Update(item);
            //    }
            //}


            //var hwdata = this.BaseRepository().FindList<GoodsWarehouseEntity>(t =>t.colorid < 10000).Result;


            //foreach (var item in hwdata)
            //{
            //    var ysdata = this.BaseRepository().FindList<GoodsAttrEntity>(t => t.goods_id == item.goods_id).Result.FirstOrDefault();

            //    item.color = ysdata.attr_name;
            //    item.colorid = ysdata.Id.Value;
            //    await this.BaseRepository().Update(item);
            //}

            var ysdata = this.BaseRepository().FindList<GoodsAttrEntity>().Result;

            foreach (var item in ysdata)
            {
                var hwdata = this.BaseRepository().FindList<GoodsWarehouseEntity>(t => t.colorid == item.Id && t.status==0).Result;
                if (hwdata!=null)
                {
                    item.count = hwdata.Count();
                    this.BaseRepository().Update(item);
                }
            }

        }
        
        #endregion

        #region 私有方法
        private Expression<Func<GoodsAttrEntity, bool>> ListFilter(GoodsAttrListParam param)
        {
            var expression = LinqExtensions.True<GoodsAttrEntity>();
            if (param != null)
            {
                if (!param.goods_id.IsNullOrZero()) {
                    expression = expression.And(t => t.goods_id == param.goods_id);
                }
                if (!param.attr_name.IsEmpty())
                {
                    expression = expression.And(t => t.attr_name.Contains(param.attr_name));
                }
            }
            return expression;
        }
        #endregion
    }
}
